import HomeAPI from "../../../repo/api/home_api";
import store from "../../../repo/store/store";
import PlanAPI from "../../../repo/api/PlanAPI";
import EvaluationAPI from "../../../repo/api/EvaluationAPI";
import EnterpriseCourseAPI from "../../../repo/api/EnterpriseCourseAPI";
import EnterpriseLevelAPI from "../../../repo/api/EnterpriseLevelAPI";
import {Toast} from "vant";
import OnlineAPI from "../../../repo/api/OnlineAPI";
import {preFixInteger} from "../../../utils/util";

let HomeMixin = {
  data() {
    return {
      banners: [],
      evaluationList: [],
      planList: [],
      categoryList: [],
      allCourseList: {},
      currCategoryId: 0,
      planCategories: {},
      courseCategories: {},
      courseIdsList: [],
      buyInfo: {},
      courseList: {},
    }
  },
  computed: {
    hsId() {
      return store.getters.hsId
    },
  },
  methods: {
    switchCategory(data) {
      this.currCategoryId = data.categoryId
      if (this.allCourseList[this.currCategoryId]) {
        this.courseList = this.allCourseList[this.currCategoryId]
      } else {
        this.fetChCourseData(this.currCategoryId, 1)
      }
    },
    fetChCourseData(id, index) {
      if (index == 1) {
        Toast.loading({
          duration: 0,
          message: '正在加载',
          forbidClick: true,
          loadingType: "spinner",
        })
      }
      EnterpriseCourseAPI.getAllSubjectByPackageId(id).then((result) => {
        this.allCourseList[id] = result.responseData.list
        this.courseList = this.allCourseList[id]
        if (index == 1) {
          Toast.clear()
        }
      })
    },
    /*async fetchDataCustom() {
      let [getCourse] = await Promise.all([
        OnlineAPI.getCourse()
      ]);
      let onlineCourseCategories = {};
      let _courseCategories = {};
      Object.keys(getCourse).map((item) => {
        if (item === '0') {
          onlineCourseCategories = getCourse[item];
          for (let key in onlineCourseCategories) {
            let list = onlineCourseCategories[key];
            EnterpriseCourseAPI.getLevelSubjectByIdsList(list).then((result) => {
              let courses = result.responseData.list;
              _courseCategories[key] = courses
            })
          }

          this.courseCategories = _courseCategories
        }
      });

    },*/
    async fetchData(data) {
      let [getInfo, getLevel] = await Promise.all([
        EnterpriseLevelAPI.getEnterpriseInfo(),
        EnterpriseLevelAPI.getGroupLevelInfo()
      ]).catch((e) => {
        console.log(e)
      })
      if (getInfo) {
        if (getLevel.responseData.buyInfo) {
          this.buyInfo = getLevel.responseData.buyInfo
        } else {
          this.buyInfo = {
            subscriptionId: 0,
          }
          Toast('请先购买团队服务');

          await this.$router.push({path: `/myOrg/${store.getters.loginType}/${this.hsId}`});
          return
        }
      } else {
        return
      }
      console.log('store.getters.hsId === ', store.getters.hsId)

      //获取Banner
      HomeAPI.getBanner(data.bannerType + preFixInteger(store.getters.hsId, 3)).then((result) => {
        let list = result.responseData.list;
        if (list && list.length > 0) {
          this.banners = list;
          return Promise.resolve(list)
        } else
          return HomeAPI.getBanner(data.bannerType).then((result) => {
            this.banners = result.responseData.list
          })
      })

      let onlineCourseCategories = store.getters['online/enterpriseCourseList'];
      let _courseCategories = {}
      for (let key in onlineCourseCategories) {
        let list = onlineCourseCategories[key]
        EnterpriseCourseAPI.getLevelSubjectByIdsList(list).then((result) => {
          let courses = result.responseData.list
          _courseCategories[key] = courses
        })
      }
      this.courseCategories = _courseCategories
      console.log('courseCategories', this.courseCategories)


      EvaluationAPI.getMyEvaluateList().then((result) => {
        this.evaluationList = result.responseData.list.slice(0, 3)
      });


      //获取星级课程列表
      PlanAPI.getPlanList().then((result) => {
        console.log('planList', result)
        let plans = result.responseData.list;
        this.planList = plans;
        let onlinePlanCategories = store.getters['online/associationPlanList'];
        console.log('onlinePlanCategories ', onlinePlanCategories)
        let _planCategories = {};
        for (let key in onlinePlanCategories) {
          let value = onlinePlanCategories[key];
          let _planList = plans.filter(plan => value.includes(plan.id));
          if (_planList == null || _planList.length === 0) {
            continue
          }
          _planCategories[key] = _planList
        }
        this.planCategories = _planCategories;
      })


      //获取分类列表
      // EnterpriseCourseAPI.getPackageList().then((result) => {
      //   let list = result.responseData.list;
      //   this.categoryList = list;
      //   let categoryId = 0;
      //   if (list && list.length > 0) {
      //     categoryId = list[0].id
      //   }
      //   //console.log(categoryId)
      //   this.currCategoryId = categoryId
      //   //根据第一个分类ID获取他的专题列表
      //   this.fetChCourseData(this.currCategoryId, 0)
      // })


    },
    //分类点击
    sortSwitch(el, idx) {
      this.switchCategory({
        'categoryId': el.id
      })
    },
  },
}

export default HomeMixin
